
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:jd_shopping/services/ScreenAdaper.dart';
import 'package:jd_shopping/RequestMethod/httpRequestMtehod.dart';
import 'package:jd_shopping/Model/FocusItemModel.dart';
import 'package:jd_shopping/HttpRequest/configDomain.dart';
import 'package:jd_shopping/Model/ProductModel.dart';

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> with AutomaticKeepAliveClientMixin {
    List _foucsList = [];
    List _hotProductList = [];
    List _bestProductList = [];
    
    @override
    bool get wantKeepAlive => true;

  @override
  void initState() {
    HttpRequestMethod.getfocus().then((response){
       var focusList = FousModel.fromJson(response.data);
       setState(() {
           this._foucsList= focusList.result;
           print(' this._foucsList----->${ this._foucsList}');
        });
     });
     HttpRequestMethod.getHotProductData().then((response){
       var focusList = ProductModel.fromJson(response.data);
       setState(() {
           this._hotProductList= focusList.result;
           print(' this._hotProductList----->${ this._hotProductList}');
        });
     });
      HttpRequestMethod.getBestProductData().then((response){
       var focusList = ProductModel.fromJson(response.data);
       setState(() {
           this._bestProductList= focusList.result;
           print(' this._bestProductList----->${ this._bestProductList}');
        });
     });
    // TODO: implement initState
   super.initState();
  }
  //轮播图
  Widget _swiperWidget() {
    return this._foucsList.length != 0 ? Container(
      child: AspectRatio(
        aspectRatio: 2 / 1,
        child: Swiper(
            itemBuilder: (BuildContext context, int index) {
               String pic=this._foucsList[index].pic;
              return new Image.network(
                "http://jd.itying.com/${pic.replaceAll('\\', '/')}",
                fit: BoxFit.fill,
              );
            },
            itemCount: this._foucsList.length,
            pagination: new SwiperPagination(),
            autoplay: true),

      ),
    ):  Container(
      child: AspectRatio(
        aspectRatio: 2 / 1,)
      );
  }

  Widget _titleWidget(value) {
    return Container(
      height: ScreenAdaper.height(32),
      margin: EdgeInsets.only(left: ScreenAdaper.width(20)),
      padding: EdgeInsets.only(left: ScreenAdaper.width(20)),
      decoration: BoxDecoration(
          border: Border(
              left: BorderSide(
        color: Colors.red,
        width: ScreenAdaper.width(10),
      ))),
      child: Text(
        value,
        style: TextStyle(color: Colors.black54),
      ),
    );
  }

  Widget _hotProductListWidget() {
    return Container(
      height: ScreenAdaper.height(234),
      padding: EdgeInsets.all(ScreenAdaper.width(20)),
      child: ListView.builder(
        scrollDirection: Axis.horizontal,
        itemBuilder: (contxt, index) {
          //处理图片
           String sPic = this._hotProductList[index].sPic;
           sPic =  ConfigDomian.domain + '/' + sPic.replaceAll('\\', '/');
          return Column(
            children: <Widget>[
              Container(
                height: ScreenAdaper.height(140),
                width: ScreenAdaper.width(140),
                margin: EdgeInsets.only(right: ScreenAdaper.width(20)),
                child: Image.network(
                    sPic,
                    fit: BoxFit.cover),
              ),
              Container(
                padding: EdgeInsets.only(top: ScreenAdaper.height(10)),
                height: ScreenAdaper.height(44),
                child: Text("¥${this._hotProductList[index].price}",style: TextStyle(color: Colors.red),),
              )
            ],
          );
        },
        itemCount: this._hotProductList.length,
      ),
    );
  }

//推荐商品
  Widget _recProductListWidget() {
    var itemWidth = (ScreenAdaper.getScreenWidth() - 30) / 2;
    return Container(
      padding: EdgeInsets.all(10),
      child: Wrap(
        runSpacing: 10,
        spacing: 10,
        children: this._bestProductList.map((value) {
          //图片
          String sPic=value.sPic;
          sPic= ConfigDomian.domain + '/' + sPic.replaceAll('\\', '/');
          return InkWell(
            child:Container(
            padding: EdgeInsets.all(10),
            width: itemWidth,
            decoration: BoxDecoration(
                border: Border.all(
                    color: Color.fromRGBO(233, 233, 233, 0.9), width: 1)),
            child: Column(
              children: <Widget>[
                Container(
                  width: double.infinity,
                  child: AspectRatio(
                    //防止服务器返回的图片大小不一致导致高度不一致问题
                    aspectRatio: 1 / 1,
                    child: Image.network(
                      sPic,
                      fit: BoxFit.cover,
                    ),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.only(top: ScreenAdaper.height(20)),
                  child: Text(
                    "${value.title}",
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(color: Colors.black54),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.only(top: ScreenAdaper.height(20)),
                  child: Stack(
                    children: <Widget>[
                      Align(
                        alignment: Alignment.centerLeft,
                        child: Text(
                          "¥${value.price}",
                          style: TextStyle(color: Colors.red, fontSize: 16),
                        ),
                      ),
                      Align(
                        alignment: Alignment.centerRight,
                        child: Text( "¥${value.oldPrice}",
                            style: TextStyle(
                                color: Colors.black54,
                                fontSize: 14,
                                decoration: TextDecoration.lineThrough)),
                      )
                    ],
                  ),
                )
              ],
            ),
            ),
            
            onTap:(){
                 print(value.sId);
                 Navigator.pushNamed(context, '/productdetaile',arguments: {
                      "sId" : value.sId,
                 });
            },
          );
        }).toList(),
      ),
    );
  }

   Widget _searchBar() {
    return AppBar(
      leading: IconButton(
          icon: Icon(Icons.center_focus_weak, size: 28, color: Colors.black87),
          onPressed: null,
        ),
        title: InkWell(
          child: Container(
            height: ScreenAdaper.height(68),
            decoration: BoxDecoration(
              color: Color.fromRGBO(233, 233, 233, 0.8),
              borderRadius: BorderRadius.circular(30)
            ),
            padding: EdgeInsets.only(left: 10),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Icon(Icons.search),
                Text("笔记本",style: TextStyle(
                  fontSize: ScreenAdaper.size(28)
                ))
              ],
            ),
          ),
          onTap: (){
              Navigator.pushNamed(context, '/search');
          },
        ),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.message, size: 28, color: Colors.black87),
            onPressed: null,
          )
        ],
    );
  }

  @override
  Widget build(BuildContext context) {
    ScreenAdaper.init(context);
    return Scaffold(
      appBar: _searchBar(),
      body:  ListView(
      children: [
        _swiperWidget(),
        SizedBox(height: ScreenAdaper.height(10)),
        _titleWidget('猜你喜欢'),
        SizedBox(height: ScreenAdaper.height(10)),
        _hotProductListWidget(),
        _titleWidget('热门推荐'),
        _recProductListWidget()
      ],
    )
      
    );
  }
}
